package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.MyFansOrMyConcernsQuery;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 关注的控制器
 */
@RestController
@RequestMapping("concerneds")
public class ConcernedController {
    @Autowired
    private IUserService userService;

    /**
     * 取消关注的资源控制器
     * @param concernedId
     * @return
     */
    @DeleteMapping("/{concernedId}")
    public JSONResult delete(@PathVariable Long concernedId){
        User user = UserContext.getCurrentUser();
        if(user != null){
            userService.deleteConcerned(user.getId(),concernedId);
        }
        return new JSONResult();
    }

    @PostMapping("/{concernedId}")
    public JSONResult save(@PathVariable Long concernedId){
        User user = UserContext.getCurrentUser();
        JSONResult result = new JSONResult();
        if (user != null) {
            try {
                userService.saveConcerned(concernedId,user.getId());
            } catch (Exception e) {
                e.printStackTrace();
                result.mark(e.getMessage());
            }
        }else {
            result.mark("你需要登录了,才能关注你喜欢的人额");
        }
        return result;
    }

    @GetMapping
    public List<Integer> allConcerneds(){
        User user = UserContext.getCurrentUser();
        if (user != null){
            return userService.selectByUserIdForConcerneds(user.getId());
        }
        return null;
    }

    /**
     * 通过被关注人的id 查询他的粉丝数量
     * @return
     * type:表示前台 让他传入一个字符串,判断是查询该用户的粉丝,还是查询他关注别人的关注数
     */
    @GetMapping("/{concernedId}")
    public Integer fans(@PathVariable Long concernedId,String type){
        //如果type为null,表示需要查询的是fans数
        if (type == null){
            return userService.selectFans(concernedId);
        }else {
            return userService.selectconcerneds(concernedId);
        }

    }

    /**
     * 获取该用户的关注人列表信息
     * userId:不是当前登录的用户ID
     * @return
     */
    @GetMapping("/concerns")
    public PageInfo listUsersConcerned(MyFansOrMyConcernsQuery qo){
        qo.setPageSize(11);
        qo.setOrderBy("c.createTime desc");
        PageInfo pageInfo=userService.listUsersConcerned(qo);
        return pageInfo;
    }


    /**
     * 获取该用户的粉丝列表信息
     * userId:不是当前登录的用户ID
     * @return
     */
    @GetMapping("/fans")
    public PageInfo listUsersFans(MyFansOrMyConcernsQuery qo){
        qo.setPageSize(11);
        qo.setOrderBy("c.createTime desc");
        PageInfo pageInfo=userService.listUsersFans(qo);
        return pageInfo;
    }

}
